package com.sgb.kjwl.view.ui.activity.demand;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDemandDetailsBinding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.DemandPublishEntity;
import com.sgb.kjwl.view.event.self.DemandEvent;
import com.sgb.kjwl.view.ui.adapter.home.MaterialSellerChildAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.demand.DemandModel3;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_CHANGE;
import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_SUC;

/**
 * Description:
 * Author CFK
 * Time
 */
public class DemandDetailsActivity extends AppActivity {
    private String no;
    private int type;
    private ActivityDemandDetailsBinding binding;
    private MaterialSellerChildAdapter materialSellerChildAdapter;
    private LinearLayoutManager linearLayoutManager;
    private DemandEvent demandEvent;
    private boolean isChange;
    @Inject
    DemandModel3 viewModel;

    public static void start(Context context, String no, int type) {
        Intent intent = new Intent();
        intent.putExtra("no", no);
        intent.putExtra("type", type);
        intent.setClass(context, DemandDetailsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder().appComponent(appComponent).homeModule(new HomeModule(this)).build().inject(this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAttrEvent(DemandEvent event) {
        if (event.getMessageTag() == EVENT_ADD_GOOGS_SUC) {
            viewModel.getNeedsList(no);
            isChange = true;
        }
    }

    @Override
    protected void initView() {
        no = getIntent().getStringExtra("no");
        type = getIntent().getIntExtra("type", 0);
        binding = DataBindingUtil.setContentView(DemandDetailsActivity.this, R.layout.activity_demand_details);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("商品需求");
        if (type == 1) {
            binding.tvCall.setOnClickListener(this);
            binding.tvCall.setVisibility(View.VISIBLE);
            binding.tvLook.setVisibility(View.GONE);
        } else {
            binding.tvEdit.setOnClickListener(this);
            binding.tvDown.setOnClickListener(this);
            binding.tvDelete.setOnClickListener(this);
            binding.llBottom.setVisibility(View.VISIBLE);
        }

        linearLayoutManager = new LinearLayoutManager(this);
        materialSellerChildAdapter = new MaterialSellerChildAdapter(this, null);
        binding.rv.setLayoutManager(linearLayoutManager);
        binding.rv.setAdapter(materialSellerChildAdapter);
    }


    @Override
    protected void init() {
        viewModel.getNeedsList(no);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                if (isChange) {
                    if (demandEvent == null) {
                        demandEvent = new DemandEvent();
                    }
                    demandEvent.setMessageTag(EVENT_ADD_GOOGS_CHANGE);
                    EventBus.getDefault().post(demandEvent);
                }
                finish();
                break;
            case R.id.tv_call:
                showDialog();
                break;
            case R.id.tv_edit:
                DemandPublishActivity.start(DemandDetailsActivity.this, viewModel.demandPublishEntity.get());
                break;
            case R.id.tv_down:
                if (binding.tvDown.getText().equals("上架")) {
                    viewModel.upDown(viewModel.demandPublishEntity.get().getNo(), "1");
                } else {
                    viewModel.upDown(viewModel.demandPublishEntity.get().getNo(), "2");
                }
                break;
            case R.id.tv_delete:
                viewModel.delete(viewModel.demandPublishEntity.get().getNo());
                break;
        }
    }

    public void setString(String string) {
        binding.tvDown.setText(string);

    }

    public void setString2(String string) {
        binding.tvLook.setText(string);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void showDialog() {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("确认拨打电话");
        tv_title.setVisibility(View.VISIBLE);
        tv_message.setText(viewModel.demandPublishEntity.get().getConcatNum());

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callPhone(viewModel.demandPublishEntity.get().getConcatNum());
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    public void setDatas(DemandPublishEntity datas) {
        binding.setViewModel(datas);
        viewModel.demandPublishEntity.set(datas);
        if ("已上架".equals(datas.getStatus())) {
            binding.tvDown.setText("下架");
        } else {
            binding.tvDown.setText("上架");
        }
        binding.tvNo.setText("需求编号：" + datas.getNo());
        binding.tvTime.setText("发布时间：" + datas.getPublishTime());
        materialSellerChildAdapter.setDatas(datas.getGoodsList());
        binding.address.setText(datas.getProvinceName() + datas.getCityName() + datas.getDistrictName() + datas.getAddressDetail());
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (isChange) {
            if (demandEvent == null) {
                demandEvent = new DemandEvent();
            }
            demandEvent.setMessageTag(EVENT_ADD_GOOGS_CHANGE);
            EventBus.getDefault().post(demandEvent);
            finish();
        }
    }
}
